/*
 * $Id$
 * $HeadURL$
 *
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.jettyrunner.server;

import java.util.regex.Pattern;

/**
 * @author Andrei Solntsev
 * @version $Revision: $ $Date: $ $Author: $
 */
public class Webapp
{
	protected static final String CLASS_VERSION = "$Revision: 1.2 $ $Author: asolntsev $";

	public static String extractContextFromPom(String pomXml)
	{
		return Pattern.compile(".*<webapp\\.name>([\\w\\d_\\-\\/]*)<\\/webapp\\.name>.*", Pattern.DOTALL)
			.matcher(pomXml)
			.replaceFirst("/$1");

		//return pomXml.replaceFirst(".*<webapp\\.name>(.*)<\\/webapp\\.name>.*", "/$1");
	}

	public static String extractContextFromWebXml(String webXml)
	{
		// Unfortunatelly this regexp find the last occurrence, but I need the first one:
		//return Pattern.compile(".*<display-name>([\\w\\d_\\-\\/]*)<\\/display-name>.*", Pattern.DOTALL)
		//	.matcher(tokens[0]).replaceFirst("/$1");

		// That's why I made this trick:
		String[] tokens = webXml.split("/display-name", 2);
		return Pattern.compile(".*<display-name>([\\w\\d_\\-\\/]*)<.*", Pattern.DOTALL)
			.matcher(tokens[0]).replaceFirst("/$1");
	}
}
